package com.ruoyi.manage.enterprise.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StrUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import com.ruoyi.manage.enterprise.domain.EcEnterpriseFile;
import com.ruoyi.manage.enterprise.domain.vo.EcEnterpriseFileVo;
import com.ruoyi.manage.enterprise.service.IEcEnterpriseFileService;
import com.ruoyi.common.utils.poi.ExcelUtil;
/**
 * 企业附件信息Controller
 *
 * @author ruoyi
 * @date 2025-08-17
 */
@Api(tags="企业附件信息控制器")
@RestController
@RequestMapping("/manage/file" )
public class EcEnterpriseFileController extends BaseController {
    /**
    * 配置表别名 默认空
    */
    private final static String TABLE_PREFIX=StrUtil.initTableAlias(null);
    @Autowired
    private IEcEnterpriseFileService ecEnterpriseFileService;

    /**
     * 查询企业附件信息列表
     */
    @ApiOperation(value = "查询企业附件信息",notes = "企业附件信息管理")
    @PreAuthorize("@ss.hasPermi('manage:file:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(EcEnterpriseFileVo ecEnterpriseFile,QueryWrapper<EcEnterpriseFile> queryWrapper) {
        setQueryCondition(ecEnterpriseFile,queryWrapper);
        startPage();
        List<EcEnterpriseFile> list = ecEnterpriseFileService.list(queryWrapper);
        return getDataTable(list);
    }
    private void setQueryCondition(EcEnterpriseFileVo ecEnterpriseFile, QueryWrapper<EcEnterpriseFile> queryWrapper) {
        if(StrUtil.isNotBlank(ecEnterpriseFile.getFileNo())){
            queryWrapper.eq(TABLE_PREFIX+"file_no" ,ecEnterpriseFile.getFileNo());
        }
        if(StrUtil.isNotBlank(ecEnterpriseFile.getFileType())){
            queryWrapper.eq(TABLE_PREFIX+"file_type" ,ecEnterpriseFile.getFileType());
        }
        if(StrUtil.isNotBlank(ecEnterpriseFile.getFileStorageMode())){
            queryWrapper.eq(TABLE_PREFIX+"file_storage_mode" ,ecEnterpriseFile.getFileStorageMode());
        }
        if(StrUtil.isNotBlank(ecEnterpriseFile.getFileUrl())){
            queryWrapper.eq(TABLE_PREFIX+"file_url" ,ecEnterpriseFile.getFileUrl());
        }
        if(ecEnterpriseFile.getVersion() != null){
            queryWrapper.eq(TABLE_PREFIX+"version" ,ecEnterpriseFile.getVersion());
        }
        if(ecEnterpriseFile.getDeleted() != null){
            queryWrapper.eq(TABLE_PREFIX+"deleted" ,ecEnterpriseFile.getDeleted());
        }
        if(ecEnterpriseFile.getEnterpriseId() != null){
            queryWrapper.eq(TABLE_PREFIX+"enterprise_id" ,ecEnterpriseFile.getEnterpriseId());
        }
        queryWrapper.orderByDesc(TABLE_PREFIX+"create_time");
    }

    /**
     * 导出企业附件信息列表
     */
    @PreAuthorize("@ss.hasPermi('manage:file:export')" )
    @PostMapping("/export" )
    public void export(HttpServletResponse response, EcEnterpriseFileVo ecEnterpriseFile,QueryWrapper<EcEnterpriseFile> queryWrapper) {
        if (CollUtil.isEmpty( ecEnterpriseFile.getIds())){
            setQueryCondition(ecEnterpriseFile,queryWrapper);
        }else {
            queryWrapper.in(TABLE_PREFIX+"id",ecEnterpriseFile.getIds());
        }
        List<EcEnterpriseFile> list = ecEnterpriseFileService.list(queryWrapper);
        ExcelUtil<EcEnterpriseFile> util = new ExcelUtil<EcEnterpriseFile>(EcEnterpriseFile. class);
        util.exportExcel(response,list, "file");
    }

    /**
     * 获取企业附件信息详细信息
     */
    @ApiOperation(value = "获取企业附件信息",notes = "企业附件信息管理")
    @PreAuthorize("@ss.hasPermi('manage:file:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(ecEnterpriseFileService.queryGetById(id));
    }

    /**
     * 新增企业附件信息
     */
    @ApiOperation(value = "新增企业附件信息",notes = "企业附件信息管理")
    @PreAuthorize("@ss.hasPermi('manage:file:add')" )
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@Validated @RequestBody EcEnterpriseFile ecEnterpriseFile) {
        return toAjax(ecEnterpriseFileService.addEcEnterpriseFile(ecEnterpriseFile) ? 1 : 0);
    }

    /**
     * 修改企业附件信息
     */
    @ApiOperation(value = "修改企业附件信息",notes = "企业附件信息管理")
    @PreAuthorize("@ss.hasPermi('manage:file:edit')" )
    @PutMapping
    public AjaxResult edit(@RequestBody EcEnterpriseFile ecEnterpriseFile) {
        return toAjax(ecEnterpriseFileService.updateEcEnterpriseFile(ecEnterpriseFile) ? 1 : 0);
    }

    /**
     * 删除企业附件信息
     */
    @ApiOperation(value = "删除企业附件信息",notes = "企业附件信息管理")
    @PreAuthorize("@ss.hasPermi('manage:file:remove')" )
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ecEnterpriseFileService.deleteEcEnterpriseFileByIds(ids) ? 1 : 0);
    }
}
